//
//  BDTextFieldAlert.m
//  BadBoy
//
//  Created by wen on 2017/4/18.
//  Copyright © 2017年 com.8xiong. All rights reserved.
//

#import "BDTextFieldAlert.h"

@interface BDTextFieldAlert ()<UITextFieldDelegate>

@property (nonatomic, strong) UITextField *textField;

@end

@implementation BDTextFieldAlert

+ (void)showWithTitle:(NSString *)title placeHolder:(NSString *)placeHolder finishBlock:(void (^)(NSString *))finishBlock{
    BDTextFieldAlert *textFieldAlert = [[BDTextFieldAlert alloc] initWithFrame:[UIApplication sharedApplication].keyWindow.bounds];
    textFieldAlert.backgroundColor = [UIColor clearColor];
    [[UIApplication sharedApplication].keyWindow addSubview:textFieldAlert];
    
    
    UIView *cutomView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 270, 200)];
    cutomView.backgroundColor= [UIColor whiteColor];
    cutomView.layer.masksToBounds = YES;
    cutomView.layer.cornerRadius = 4;
    
    UIView *bkView = [[UIView alloc] initWithFrame:[UIApplication sharedApplication].keyWindow.bounds];
    cutomView.center = bkView.center;
    [bkView addSubview:cutomView];
    cutomView.alpha = 0;
    bkView.backgroundColor = [UIColor clearColor];
    [textFieldAlert addSubview:bkView];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 270, 40)];
    titleLabel.text = title;
    titleLabel.font = [UIFont systemFontOfSize:17];
    titleLabel.textColor = [UIColor colorWithHexString:@"#4d7aad"];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [cutomView addSubview:titleLabel];
    
    textFieldAlert.textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 0, 230, 40)];
    textFieldAlert.textField.font = [UIFont systemFontOfSize:15];
    textFieldAlert.textField.backgroundColor = [UIColor colorWithHexString:@"#ececec"];
    textFieldAlert.textField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 15, 10)];
    textFieldAlert.textField.leftViewMode = UITextFieldViewModeAlways;
    textFieldAlert.textField.centerY = cutomView.height / 2.0;
    textFieldAlert.textField.placeholder = placeHolder;
    textFieldAlert.textField.returnKeyType = UIReturnKeyDone;
    textFieldAlert.textField.delegate = textFieldAlert;
    [cutomView addSubview:textFieldAlert.textField];
    
    UIButton *cancleButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancleButton setTitle:ASLocalizedString(@"取消")forState:UIControlStateNormal];
    [cancleButton setTitleColor:[UIColor colorWithHexString:@"#808080"] forState:UIControlStateNormal];
    cancleButton.titleLabel.font = [UIFont systemFontOfSize:16];
    cancleButton.size = CGSizeMake(cutomView.width / 2.0, 45);
    cancleButton.bottom = cutomView.height;
    [cutomView addSubview:cancleButton];
    
    UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [doneButton setTitle:ASLocalizedString(@"确定")forState:UIControlStateNormal];
    [doneButton setTitleColor:[UIColor colorWithHexString:@"#4d7aad"] forState:UIControlStateNormal];
    doneButton.titleLabel.font = [UIFont systemFontOfSize:16];
    doneButton.size = CGSizeMake(cutomView.width / 2.0, 45);
    doneButton.bottom = cutomView.height;
    doneButton.right = cutomView.width;
    [cutomView addSubview:doneButton];
    
    UIView *littLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, doneButton.height)];
    littLine.backgroundColor = kBDColorGray;
    littLine.bottom = doneButton.bottom;
    littLine.centerX = cutomView.width / 2.0;
    [cutomView addSubview:littLine];
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, cutomView.width, 1)];
    line.backgroundColor = kBDColorGray;
    line.bottom = doneButton.top;
    [cutomView addSubview:line];
    
    [UIView animateWithDuration:0.3 animations:^{
        bkView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
        cutomView.alpha = 1;
    } completion:^(BOOL finished) {
        
    }];
    
    [[cancleButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [UIView animateWithDuration:0.3 animations:^{
            bkView.backgroundColor = [UIColor clearColor];
            cutomView.alpha = 0;
        } completion:^(BOOL finished) {
            [textFieldAlert removeFromSuperview];
        }];
    }];
    [[doneButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [UIView animateWithDuration:0.3 animations:^{
            bkView.backgroundColor = [UIColor clearColor];
            cutomView.alpha = 0;
        } completion:^(BOOL finished) {
            [textFieldAlert removeFromSuperview];
            if (finishBlock) {
                finishBlock(textFieldAlert.textField.text);
            }
        }];
    }];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

@end
